
<!DOCTYPE html>
<html lang="zh-CN" class="loading">
<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>IONIC 2 开发笔记 - 三·钻 TriDiamond</title>
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate">
    <meta name="keywords" content="TriDiamond Obsidian,"> 
    <meta name="description" content="因为IONIC2才刚刚开始进入正式版, 中文文档基本都是不全的, 我现在开发都是在看英文文档在开发的过程中遇到了很多文档没有描述的问题, 而且开发的过程中遇到一些框架本身没有完善的功能和存在的BUG,"> 
    <meta name="author" content="三·钻 TriDiamond"> 
    <link rel="alternative" href="atom.xml" title="三·钻 TriDiamond" type="application/atom+xml"> 
    <link rel="icon" href="/img/favicon.png"> 
    <link href="https://fonts.loli.net/css?family=Roboto+Mono|Rubik&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="//at.alicdn.com/t/font_1429596_nzgqgvnmkjb.css">
    <link rel="stylesheet" href="//cdn.bootcss.com/animate.css/3.7.2/animate.min.css">
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/css/share.min.css">
    <link rel="stylesheet" href="//cdn.bootcss.com/codemirror/5.48.4/codemirror.min.css">
    <link rel="stylesheet" href="//cdn.bootcss.com/codemirror/5.48.4/theme/dracula.css">
    <link rel="stylesheet" href="/css/obsidian.css">
    <link rel="stylesheet" href="/css/ball-atom.min.css">
</head>
</html>

<body class="loading">
    <div class="loader">
        <div class="la-ball-atom la-2x">
            <div></div>
            <div></div>
            <div></div>
            <div></div>
        </div>
    </div>
    <span id="config-title" style="display:none">三·钻 TriDiamond</span>
    <div id="loader"></div>
    <div id="single">
    <div class="scrollbar gradient-bg-rev"></div>
<div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <div class="navigation animated fadeIn fast delay-1s">
        <img id="home-icon" class="icon-home" src="/img/favicon.png" alt="" data-url="https://tridiamond.me">
        <div id="play-icon" title="Play/Pause" class="iconfont icon-play"></div>
        <h3 class="subtitle">IONIC 2 开发笔记</h3>
        <div class="social">
            <!--        <div class="like-icon">-->
            <!--            <a href="javascript:;" class="likeThis active"><span class="icon-like"></span><span class="count">76</span></a>-->
            <!--        </div>-->
            <div>
                <div class="share">
                    
                        <a href="javascript:;" class="iconfont icon-share1"></a>
                        <div class="share-component-cc" data-disabled="facebook,douban,linkedin,diandian,tencent,google"></div>
                    
                </div>
            </div>
        </div>
    </div>
</div>

    <div class="section">
        <div class=article-header-wrapper>
    <div class="article-header">
        <div class="article-cover animated fadeIn" style="
            animation-delay: 600ms;
            animation-duration: 1.2s;
            background-image: 
                radial-gradient(ellipse closest-side, rgba(0, 0, 0, 0.65), #100e17),
                url(https://res.cloudinary.com/tridiamond/image/upload/v1571186961/blog/ionic_cover_hvzkwo.png);">
        </div>
        <div class="else">
            <p class="animated fadeInDown">
                
                <a href="/categories/Ionic"><b>「
                    </b>IONIC<b> 」</b></a>
                
                二月 25, 2017
            </p>
            <h3 class="post-title animated fadeInDown"><a href="/2017/02/25/ionic/dev-note.html" title="IONIC 2 开发笔记">IONIC 2 开发笔记</a>
            </h3>
            
            <p class="post-count animated fadeInDown">
                
                <span>
                    <b class="iconfont icon-text2"></b> <i>文章字数</i>
                    4.1k
                </span>
                
                
                <span>
                    <b class="iconfont icon-timer__s"></b> <i>阅读约需</i>
                    4 mins.
                </span>
                
                
                
                <span id="busuanzi_container_page_pv">
                    <b class="iconfont icon-read"></b> <i>阅读次数</i>
                    <span id="busuanzi_value_page_pv">0</span>
                </span>
                
            </p>
            
            
            <ul class="animated fadeInDown post-tags-list"><li class="animated fadeInDown post-tags-list-item"><a class="animated fadeInDown post-tags-list-link" href="/tags/Angular-2/">Angular 2</a></li><li class="animated fadeInDown post-tags-list-item"><a class="animated fadeInDown post-tags-list-link" href="/tags/Hybird-App/">Hybird App</a></li><li class="animated fadeInDown post-tags-list-item"><a class="animated fadeInDown post-tags-list-link" href="/tags/Ionic-2/">Ionic 2</a></li></ul>
            
        </div>
    </div>
</div>

<div class="screen-gradient-after">
    <div class="screen-gradient-content">
        <div class="screen-gradient-content-inside">
            <div class="bold-underline-links screen-gradient-sponsor">
                <p>
                    <span class="animated fadeIn delay-1s">记录了所有IONIC 2开发时踩过的坑</span>
                </p>
            </div>
        </div>
    </div>
</div>

<div class="article">
    <div class='main'>
        <div class="content markdown animated fadeIn">
            <p>因为IONIC2才刚刚开始进入正式版, 中文文档基本都是不全的, 我现在开发都是在看英文文档<br>在开发的过程中遇到了很多文档没有描述的问题, 而且开发的过程中遇到一些框架本身没有完善的功能和存在的BUG</p>
<p>我会在这个日记里面记录一些文档没有写的, 和我开发过程中遇到的一些坑和经验, 希望可以帮助那些刚刚接触IONIC2的程序猿们!</p>
<h2 id="APP配置"><a href="#APP配置" class="headerlink" title="APP配置"></a>APP配置</h2><h3 id="域名配置"><a href="#域名配置" class="headerlink" title="域名配置"></a>域名配置</h3><p>因为跨域问题，在开发时如果要用到本地环境进行开发(ionic serve)，必须配置proxy</p>
<ul>
<li>Proxy位于项目根目录下的 <strong>ionic.config.json</strong></li>
</ul>
<ul>
<li>只需要把<strong>proxyUrl</strong>改为你本地环境的API地址</li>
</ul>
<pre><code class="javascript">{
  &quot;name&quot;: &quot;rlph&quot;,
  &quot;app_id&quot;: &quot;&quot;,
  &quot;v2&quot;: true,
  &quot;typescript&quot;: true,
  &quot;proxies&quot;: [
    {
      &quot;path&quot;: &quot;/api&quot;,
      &quot;proxyUrl&quot;: &quot;http://api.dev&quot;
    }
  ]
}</code></pre>
<ul>
<li>然后把API地址的常量制定为<strong>/api/</strong>这个proxy</li>
</ul>
<ul>
<li>在根目录下<strong>/src/config.ts</strong>里面把<strong>“API_SERVER”</strong>的值改为<strong>“/api/“</strong></li>
</ul>
<pre><code>  export let data = {
      &quot;API_SERVER&quot; : &quot;/api/&quot;
  }</code></pre><h3 id="上线APP配置"><a href="#上线APP配置" class="headerlink" title="上线APP配置"></a>上线APP配置</h3><ul>
<li>首先你需要配置真是服务器API地址<br>配置API地址是在根目录下<strong>/src/config.ts</strong>里面把<strong>“API_SERVER”</strong>的值改为线上API地址</li>
</ul>
<pre><code>  export let data = {
      &quot;API_SERVER&quot; : &quot;http://api.domain.com/&quot;
  }</code></pre><h2 id="开发常见问题"><a href="#开发常见问题" class="headerlink" title="开发常见问题"></a>开发常见问题</h2><h3 id="APP-run-失败"><a href="#APP-run-失败" class="headerlink" title="APP run 失败"></a>APP run 失败</h3><p>当运行<strong>ionic run android</strong>的时候可能会遇到该报错：</p>
<pre><code>Error: Failed to install apk to device: [  1%] /data/local/tmp/android-debug.apk
[  2%] /data/local/tmp/android-debug.apk
...
[100%] /data/local/tmp/android-debug.apk
        pkg: /data/local/tmp/android-debug.apk
Failure [INSTALL_FAILED_UPDATE_INCOMPATIBLE]</code></pre><ul>
<li>此问题是因为已有签名的APP存在手机上， 需要想删除该APP才能安装测试（debug）版的apk</li>
<li>在cmd运行以下代码即可解决问题：</li>
</ul>
<pre><code>adb uninstall my.package.id</code></pre><h3 id="Ionic-2-自带的native文件上传-FILE-TRANSFER-插件无法获取成功返回内容"><a href="#Ionic-2-自带的native文件上传-FILE-TRANSFER-插件无法获取成功返回内容" class="headerlink" title="Ionic 2 自带的native文件上传(FILE TRANSFER)插件无法获取成功返回内容"></a>Ionic 2 自带的native文件上传(FILE TRANSFER)插件无法获取成功返回内容</h3><p>这个是Ionic 2 核心代码里面的一个BUG, 在一下版本下是有问题的<br><strong>Ionic CLI Version: 2.2.1</strong></p>
<ul>
<li>首先找到项目根目录下以下路径里面的<strong>filetransfer.d.ts</strong>文件</li>
</ul>
<pre><code>node_modules\ionic-native\dist\es5\plugins\filetransfer.d.ts
node_modules\ionic-native\dist\esm\plugins\filetransfer.d.ts</code></pre><ul>
<li>分别修改以上两个文件里面的代码</li>
</ul>
<pre><code>//把这一行:
upload(fileUrl: string, url: string, options?: FileUploadOptions, trustAllHosts?: boolean): Promise&lt;FileUploadResult | FileTransferError&gt;
//改为: 
upload(fileUrl: string, url: string, options?: FileUploadOptions, trustAllHosts?: boolean): Promise&lt;FileUploadResult&gt;</code></pre><h3 id="在安卓下使用相册选择时-返回的图片路径不能再显示问题"><a href="#在安卓下使用相册选择时-返回的图片路径不能再显示问题" class="headerlink" title="在安卓下使用相册选择时, 返回的图片路径不能再显示问题"></a>在安卓下使用相册选择时, 返回的图片路径不能再显示问题</h3><ul>
<li>首先在这个例子使用的是cordova-plugin-camera组件(Cordova的相机插件)</li>
<li>首先引入需要的类</li>
<li>FilePath 这个类就是用来修复安卓上图片URI的问题的</li>
</ul>
<pre><code class="typescript">import { Camera, File, FilePath } from &#39;ionic-native&#39;;</code></pre>
<ul>
<li>使用 FilePath.resolveNativePath(imagePath) 这个方法来纠正图片URI</li>
</ul>
<pre><code class="typescript">Camera.getPicture(options).then((imagePath) =&gt; {
 // 特殊安卓图片库的处理
 this.nativeFilePath = imagePath;
 if (this.pl.is(&#39;android&#39;) &amp;&amp; sourceType === Camera.PictureSourceType.PHOTOLIBRARY) {
   FilePath.resolveNativePath(imagePath)
   .then(filePath =&gt; {
       this.nativeFilePath = filePath;
       let currentName = imagePath.substring(imagePath.lastIndexOf(&#39;/&#39;) + 1, imagePath.lastIndexOf(&#39;?&#39;));
   });
 } else {
   var currentName = imagePath.substr(imagePath.lastIndexOf(&#39;/&#39;) + 1);
 }
}, err =&gt; {
 // this.presentToast(&#39;选择图片失败.&#39;);
});</code></pre>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls"
                data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
            <ul id="audio-list" style="display:none">
                
                
                <li title='0' data-url='/statics/chengdu.mp3'></li>
                
                    
            </ul>
            
            
            
    <div id='gitalk-container' class="comment link"
        data-ae='true'
        data-ci='ec894e2b66f752e8b7fb'
        data-cs='3ccc2e92bb350688fe2c2dc2930189b62622bfb1'
        data-r='blog-comments'
        data-o='TriDiamond'
        data-a='TriDiamond'
        data-d=''
    >留言</div>


            
            
        </div>
        <div class="sidebar">
            <div class="box animated fadeInRight">
                <div class="subbox">
                    <img src="https://res.cloudinary.com/tridiamond/image/upload/v1573019751/TriDiamond_logo_ui_xeublz.jpg" height=300 width=300></img>
                    <p>三·钻 TriDiamond</p>
                    <span>Think like an artist, develop like an artisan</span>
                    <dl>
                        <dd><a href="https://github.com/TriDiamond" target="_blank"><span
                                    class=" iconfont icon-github"></span></a></dd>
                        <dd><a href="https://twitter.com/TriDiamond6" target="_blank"><span
                                    class=" iconfont icon-twitter"></span></a></dd>
                        <dd><a href="https://stackoverflow.com/users/7602324/tridiamond?tab=profile" target="_blank"><span
                                    class=" iconfont icon-stack-overflow"></span></a></dd>
                    </dl>
                </div>
                <ul>
                    <li><a href="/">14 <p>文章</p></a></li>
                    <li><a href="/categories">8 <p>分类</p></a></li>
                    <li><a href="/tags">20 <p>标签</p></a></li>
                </ul>
            </div>
            
            
            
            <div class="box sticky animated fadeInRight faster">
                <div id="toc" class="subbox">
                    <h4>目录</h4>
                    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#APP配置"><span class="toc-number">1.</span> <span class="toc-text">APP配置</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#域名配置"><span class="toc-number">1.1.</span> <span class="toc-text">域名配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#上线APP配置"><span class="toc-number">1.2.</span> <span class="toc-text">上线APP配置</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#开发常见问题"><span class="toc-number">2.</span> <span class="toc-text">开发常见问题</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#APP-run-失败"><span class="toc-number">2.1.</span> <span class="toc-text">APP run 失败</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Ionic-2-自带的native文件上传-FILE-TRANSFER-插件无法获取成功返回内容"><span class="toc-number">2.2.</span> <span class="toc-text">Ionic 2 自带的native文件上传(FILE TRANSFER)插件无法获取成功返回内容</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#在安卓下使用相册选择时-返回的图片路径不能再显示问题"><span class="toc-number">2.3.</span> <span class="toc-text">在安卓下使用相册选择时, 返回的图片路径不能再显示问题</span></a></li></ol></li></ol>
                </div>
            </div>
            
            
        </div>
    </div>
</div>

    </div>
</div>
    <div id="back-to-top" class="animated fadeIn faster">
        <div class="flow"></div>
        <span class="percentage animated fadeIn faster">0%</span>
        <span class="iconfont icon-top02 animated fadeIn faster"></span>
    </div>
</body>
<footer>
    <p class="copyright" id="copyright">
        &copy; 2019
        <span class="gradient-text">
            三·钻 TriDiamond
        </span>.
        Powered by <a href="http://hexo.io/" title="Hexo" target="_blank" rel="noopener">Hexo</a>
        Theme
        <span class="gradient-text">
            <a href="https://github.com/TriDiamond/hexo-theme-obsidian" title="Obsidian" target="_blank" rel="noopener">Obsidian</a>
        </span>
        <small><a href="https://github.com/TriDiamond/hexo-theme-obsidian/blob/master/CHANGELOG.md" title="v1.4.3" target="_blank" rel="noopener">v1.4.3</a></small>
    </p>
</footer>

<script type="text/javascript" src="https://cdn.bootcss.com/mathjax/2.7.6/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<script>
  MathJax.Hub.Config({
    "HTML-CSS": {
      preferredFont: "TeX",
      availableFonts: ["STIX", "TeX"],
      linebreaks: {
        automatic: true
      },
      EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50)
    },
    tex2jax: {
      inlineMath: [
        ["$", "$"],
        ["\\(", "\\)"]
      ],
      processEscapes: true,
      ignoreClass: "tex2jax_ignore|dno",
      skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    },
    TeX: {
      noUndefined: {
        attributes: {
          mathcolor: "red",
          mathbackground: "#FFEEEE",
          mathsize: "90%"
        }
      },
      Macros: {
        href: "{}"
      }
    },
    messageStyle: "none"
  });
</script>
<script>
  function initialMathJax() {
    MathJax.Hub.Queue(function () {
      var all = MathJax.Hub.getAllJax(),
        i;
      // console.log(all);
      for (i = 0; i < all.length; i += 1) {
        console.log(all[i].SourceElement().parentNode)
        all[i].SourceElement().parentNode.className += ' has-jax';
      }
    });
  }

  function reprocessMathJax() {
    if (typeof MathJax !== 'undefined') {
      MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
    }
  }
</script>



    <link rel="stylesheet" href="//cdn.bootcss.com/gitalk/1.5.0/gitalk.min.css">
    <script src="//cdn.bootcss.com/gitalk/1.5.0/gitalk.min.js"></script>

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/obsidian.js"></script>
<script src="/js/jquery.truncate.js"></script>
<script src="/js/search.js"></script>
<script src="//cdn.bootcss.com/typed.js/2.0.10/typed.min.js"></script>
<script src="//cdn.bootcss.com/blueimp-md5/2.12.0/js/md5.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/js/social-share.min.js"></script>

<script src="https://cdn.bootcss.com/codemirror/5.48.4/codemirror.min.js"></script>

    <script src="//cdn.bootcss.com/codemirror/5.48.4/mode/javascript/javascript.min.js"></script>

    <script src="//cdn.bootcss.com/codemirror/5.48.4/mode/css/css.min.js"></script>

    <script src="//cdn.bootcss.com/codemirror/5.48.4/mode/xml/xml.min.js"></script>

    <script src="//cdn.bootcss.com/codemirror/5.48.4/mode/htmlmixed/htmlmixed.min.js"></script>

    <script src="//cdn.bootcss.com/codemirror/5.48.4/mode/clike/clike.min.js"></script>

    <script src="//cdn.bootcss.com/codemirror/5.48.4/mode/php/php.min.js"></script>

    <script src="//cdn.bootcss.com/codemirror/5.48.4/mode/shell/shell.min.js"></script>

    <script src="//cdn.bootcss.com/codemirror/5.48.4/mode/python/python.min.js"></script>



    <script src="/js/busuanzi.min.js"></script>
    <script>
        $(document).ready(function () {
            if ($('span[id^="busuanzi_"]').length) {
                initialBusuanzi();
            }
        });
    </script>


<link rel="stylesheet" href="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe.min.css">
<link rel="stylesheet" href="//cdn.bootcss.com/photoswipe/4.1.3/default-skin/default-skin.min.css">
<script src="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe.min.js"></script>
<script src="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe-ui-default.min.js"></script>

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>



    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="//www.googletagmanager.com/gtag/js?id=UA-149874671-1"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());

        gtag('config', 'UA-149874671-1');
    </script>





<script>
    function initialTyped () {
        var typedTextEl = $('.typed-text');
        if (typedTextEl && typedTextEl.length > 0) {
            var typed = new Typed('.typed-text', {
                strings: ["Think like an artist, develop like an artisan", "艺术家思维去思考问题，工匠创造精神去开发"],
                typeSpeed: 90,
                loop: true,
                loopCount: Infinity,
                backSpeed: 20,
            });
        }
    }

    if ($('.article-header') && $('.article-header').length) {
        $(document).ready(function () {
            initialTyped();
        });
    }
</script>




</html>
